<?php
/**
 * $Id$
 *  @package	Joomla
 *  @author Daniel Eliasson
* 	@copyright	Copyright (C) 2010 Stilero. All rights reserved.
* 	@license	GNU/GPL, see LICENSE.php
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access'); 

class VmcompetitionControllerVmcompetition extends VmcompetitionController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
	    parent::__construct();
	 
	    // Register Extra tasks
	    //$this->registerTask( 'add'  ,     'edit' );
	}

	function submit()
	{
	global $mainframe;
	
	// Check for request forgeries
	JRequest::checkToken() or jexit( 'Invalid Token' );
	
	
	// Initialize some variables
	$db			= & JFactory::getDBO();
	$SiteName	= $mainframe->getCfg('sitename');
	
	$default	= JText::sprintf( 'MAILENQUIRY', $SiteName );
	$contestId	= JRequest::getInt( 'id',			0,			'post' );
	
	$name		= JRequest::getVar( 'name',			'',			'post' );
	$email		= JRequest::getVar( 'email',		'',			'post' );
	$linkurl	= JRequest::getVar( 'linkurl',		'',			'post' );
	$comment		= JRequest::getVar( 'text',	'',			'post' );
	$emailCopy	= JRequest::getInt( 'id', 	0,			'post' );
	
	 // load the contact details
	$model		= &$this->getModel('contact');
	
	/*
	// query options
	$qOptions['id']	= $contactId;
	$contact		= $model->getContact( $qOptions );
	
	if($contact->email_to == '' && $contact->user_id != 0)
	{
		$contact_user = JUser::getInstance($contact->user_id);
		$contact->email_to = $contact_user->get('email');
	}
	*/
	
	/*
	 * If there is no valid email address or message body then we throw an
	 * error and return false.
	 */
	jimport('joomla.mail.helper');
	if (!$email || !$comment || (JMailHelper::isEmailAddress($email) == false))
	{
		$this->setError(JText::_('CONTACT_FORM_NC'));
		$this->display();
		return false;
	}
	
    $model = $this->getModel('contestant');
    
 
    if ($model->store()) {
        $msg = JText::_( 'Contestant Saved!' );
    } else {
        $msg = JText::_( 'Error Saving Contestant' );
    }
 
    // Check the table in so it can be edited.... we are done with it anyway
    $link = 'index.php?option=com_vmcompetition&controller=vmcompetition';
    $this->setRedirect($link, $msg);

	/*
	// Contact plugins
	JPluginHelper::importPlugin( 'contact' );
	$dispatcher	=& JDispatcher::getInstance();
	
	// Input validation
	if  (!$this->_validateInputs( $contact, $email, $subject, $body ) ) {
		JError::raiseWarning( 0, $this->getError() );
		return false;
	}
	
	// Custom handlers
	$post		= JRequest::get( 'post' );
	$results	= $dispatcher->trigger( 'onValidateContact', array( &$contact, &$post ) );
	
	foreach ($results as $result)
	{
		if (JError::isError( $result )) {
			return false;
		}
	}
	
	// Passed Validation: Process the contact plugins to integrate with other applications
	$results	= $dispatcher->trigger( 'onSubmitContact', array( &$contact, &$post ) );
	
	$pparams = &$mainframe->getParams('com_contact');
	if (!$pparams->get( 'custom_reply' ))
	{
		$MailFrom 	= $mainframe->getCfg('mailfrom');
		$FromName 	= $mainframe->getCfg('fromname');
	
		// Prepare email body
		$prefix = JText::sprintf('ENQUIRY_TEXT', JURI::base());
		$body 	= $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);
	
		$mail = JFactory::getMailer();
	
		$mail->addRecipient( $contact->email_to );
		$mail->setSender( array( $email, $name ) );
		$mail->setSubject( $FromName.': '.$subject );
		$mail->setBody( $body );
	
		$sent = $mail->Send();
	
		
		// parameter check
		$params = new JParameter( $contact->params );
		$emailcopyCheck = $params->get( 'show_email_copy', 0 );
	
		// check whether email copy function activated
		if ( $emailCopy && $emailcopyCheck )
		{
			$copyText 		= JText::sprintf('Copy of:', $contact->name, $SiteName);
			$copyText 		.= "\r\n\r\n".$body;
			$copySubject 	= JText::_('Copy of:')." ".$subject;
	
			$mail = JFactory::getMailer();
	
			$mail->addRecipient( $email );
			$mail->setSender( array( $MailFrom, $FromName ) );
			$mail->setSubject( $copySubject );
			$mail->setBody( $copyText );
	
			$sent = $mail->Send();
		}
	}
	*/
	
	$msg = JText::_( 'Thanks you for your e-mail');
	$link = JRoute::_('index.php?option=com_vmcompetition&view=vmcompetition', false);
	$this->setRedirect($link, $msg);
	}
	
	
}
?>
